﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace FaultDiagnosisSystem.Controllers
{
    /*** Index ***/
    public partial class RealTimeFaultController : Controller
    {
        // GET: RealTimeFault
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.ComponentSelectList = BuildComponentSelectWithAll();
            //ViewBag.DateSelectList = BuildDateSelect();
            ViewBag.LastDay= new BLL.RealTimeFaultDataDaily().GetLastDay().ToString("yyyy-MM-dd");

            return View();
        }
        

        /// <summary>
        /// 构建日期下拉菜单
        /// </summary>
        /// <returns></returns>
        private IList<SelectListItem> BuildDateSelect()
        {
            var lastDayOfRealTime = new BLL.RealTimeFaultDataDaily().GetLastDay();
            var dateSelectList = new List<SelectListItem>();
            dateSelectList.Add(new SelectListItem()
            {
                Text = lastDayOfRealTime.ToString("yyyy-MM-dd"),
                Value = lastDayOfRealTime.ToString("yyyy-MM-dd"),
                Selected = true
            });
            for (int i = -1; i > -7; i--)
            {
                var day = lastDayOfRealTime.AddDays(i);
                dateSelectList.Add(new SelectListItem() { Text = day.ToString("yyyy-MM-dd"), Value = day.ToString("yyyy-MM-dd") });
            }

            return dateSelectList;
        }

        /// <summary>
        /// 构建部件,部件项包含全部
        /// </summary>
        /// <returns></returns>
        private IList<SelectListItem> BuildComponentSelectWithAll()
        {
            var componentList = new BLL.Component().GetListWithSensorData();
            if (componentList != null && componentList.Count != 0)
            {
                var list = componentList.Select(
                   c => new SelectListItem
                   {
                       Text = c.ComponentName,
                       Value = c.ComponentId.ToString()
                   }).ToList();
                list.Insert(0, new SelectListItem() { Text = "全部", Value = "0", Selected = true });
                return list;
            }
            else
            {
                return new List<SelectListItem>() ;
            }
        }


        /// <summary>
        /// 获取指定日期的组件故障json数据
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        public JsonResult GetComponentFaultData(string day)
        {
            var list = new BLL.RealTimeFaultDataDaily().GetList(DateTime.Parse(day));
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取最近一天的数据,起始时间为:当前实际时间-32hours(考虑8小时延迟)
        /// </summary>
        /// <returns></returns>
        public JsonResult GetComponentFaultDataOfLastDay()
        {
            var list = new BLL.RealTimeFaultDataDaily().GetListOfLastDay();
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 指定日期和风机，获取主传动链下的传感器十分钟数据
        /// </summary>
        /// <param name="day"></param>
        /// <param name="turbineId"></param>
        /// <returns></returns>
        public JsonResult GetCom5_SensorData(string day, int turbineId)
        {
            var list = new BLL.TenMeanData().GetCom5_ViewList(DateTime.Parse(day), turbineId);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 指定日期和风机，获取主传动链下的传感器十分钟数据
        /// </summary>
        /// <param name="day"></param>
        /// <param name="turbineId"></param>
        /// <returns></returns>
        public JsonResult GetCom5_SensorDataOfLastDay(int turbineId)
        {
            var list = new BLL.TenMeanData().GetCom5_ViewListOfLastDay(turbineId);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
    }



    /*** TenMeanCompare ***/
    public partial class RealTimeFaultController : Controller
    {
        // GET: RealTimeFault/TenMeanCompare
        public ActionResult TenMeanCompare(string date, string componentId, string sensorId)
        {
            //设置初始参数
            ViewBag.ComponentId = componentId == null ? "" : componentId;
            ViewBag.SensorId = sensorId == null ? "" : sensorId;
            ViewBag.Date = date == null ? "" : date;

            //设置组件下拉菜单
            ViewBag.ComponentSelectList = BuildComponentSelect();
            //设置日期下拉菜单
            var dateList = BuildDateSelect();
            ViewBag.DateSelectList = dateList;
            ViewBag.LastDay= new BLL.RealTimeFaultDataDaily().GetLastDay().ToString("yyyy-MM-dd");

            //部件：传感器字典
            var dict = new BLL.Component().GetDictWithSensorData();
            ViewBag.ComponentDict = dict;
            return View();
        }

        /// <summary>
        /// 构建部件下拉列表,部件项不包含全部
        /// </summary>
        /// <returns></returns>
        private IList<SelectListItem> BuildComponentSelect()
        {
            var componentList = new BLL.Component().GetListWithSensorData();
            if (componentList != null && componentList.Count != 0)
            {
                var list = componentList.Select(
                   c => new SelectListItem
                   {
                       Text = c.ComponentName,
                       Value = c.ComponentId.ToString()
                       //,Selected=c.ComponentId==5?true:false  //暂时将传动链默认选中
                   }).ToList();
                return list;
            }
            else
            {
                return new List<SelectListItem>();
            }
        }


        public JsonResult GetSensorData(DateTime? day)
        {
            var dict = new Dictionary<string, List<Models.DataModels.v_TenMeanData>>();
            if (day != null)
            {
                dict = new BLL.TenMeanData().GetTurbineViewDict(day.GetValueOrDefault());
            }
            return Json(dict, JsonRequestBehavior.AllowGet);
        }


        public JsonResult GetSensorDataOfLastDay()
        {
            var dict = new Dictionary<string, List<Models.DataModels.v_TenMeanData>>();

                dict = new BLL.TenMeanData().GetTurbineViewDict2(DateTime.Now.AddHours(-32));   //当前实际时间-24-8

            return Json(dict, JsonRequestBehavior.AllowGet);
        }
    }
}